<template>
	<view style="display: flex;flex-direction: column;align-items: center;">
		<view class="headerWrapper">
			<view class="titleWrapper">
				{{nodeObj.title||""}}
			</view>
			<view class="detail text-2line-ellipsis">
				{{nodeObj.briefly||""}}
			</view>
			<view class="content">
				<view class="item">
					{{nodeObj.pageViews||"0"}}
					<view class="text">
						阅读次数
					</view>
				</view>
				<view class="item">
					{{nodeObj.readerNumber||"0"}}
					<view class="text">
						阅读人数
					</view>
				</view>
				<view class="item">
					{{nodeObj.forwardTimes||"0"}}
					<view class="text">
						转发次数
					</view>
				</view>
			</view>
		</view>
		<view class="customerWrapper">
			<view class="header">
				<view class="left">
					<view class="icon">
						
					</view>
					访客列表
				</view>
				<view class="right" @click="onShowSortType">
					{{sortList[0][sortIndex].label}}
					<view class="icon">

					</view>
				</view>
			</view>
			<template v-if="customerListObj.list&&customerListObj.list.length>0">

				<view v-for="(item,index) in customerListObj.list" class="customerItem"
					@click="gotoCustomerDetail(item)">
					<image src="@/static/image/icon_avatar.png" class="left" mode=""></image>
					<view class="right">
						<view class="top">
							{{item.name||item.nickname}}
							<view class="icon">

							</view>
						</view>
						<view class="middle">
							<view class="preview">
								访问次数
								<text class="text">
									{{item.pageViews}}
								</text>
								次
							</view>
							<view class="time">
								累计停留时长
								<template v-if="item.staySeconds>600">
									<text class="text">
										{{parseInt(item.staySeconds/60)}}
									</text>
									分钟
								</template>
								<template v-else>
									<text class="text">
										{{item.staySeconds}}
									</text>
									秒
								</template>
								
							</view>
						</view>
						<view class="bottom">
							最后访问时间
							<text style="margin-left: 20rpx;">{{item.lastVisitTime}}</text>
						</view>
					</view>
				</view>

			</template>


			<u-empty v-else mode="list" :marginTop="'150'" :text="'暂无数据'" :textSize="'20'" :mode="'history'">
			</u-empty>
		</view>

		<u-picker :show="showSort" ref="uPicker" :columns="sortList" keyName="label" @close="showSort=false"
			itemHeight="88" :defaultIndex="[Number(sortIndex)]" @cancel="showSort=false" @confirm="changeHandler">
		</u-picker>
	</view>
</template>

<script>
	const api = require("@/static/js/api.js")
	const utils = require("@/static/js/utils.js")
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex';
	let that = null



	export default {
		data() {
			return {
				nodeId: "",
				nodeObj: {},
				customerListObj: {
					list: [],
					loadStatus: "loading",
					pageIndex: 1,
					curPageIndex: 0,
				},
				showSort: false,
				sortIndex: 0,
				sortList: [
					[{
							key: 0,
							label: "访问时间降序"
						},
						{
							key: 1,
							label: "访问时间升序"
						},
						{
							key: 2,
							label: "停留时长降序"
						},
						{
							key: 3,
							label: "停留时长升序"
						}
					]
				]
			};
		},

		computed: {
			...mapState({
				hasLogin: state => state.userInfo.hasLogin,
				isAuthPhone: state => state.userInfo.isAuthPhone,
				isAuthUserInfo: state => state.userInfo.isAuthUserInfo,
				isSeller: state => state.userInfo.isSeller,
				isReferrer: state => state.userInfo.isReferrer,
				sellerObj: state => state.userInfo.sellerObj,
				buyerObj: state => state.userInfo.buyerObj,
				shopInfo: state => state.userInfo.shopInfo,
			}),
		},

		onLoad(options) {
			that = this;
			if (options.id) {
				that.nodeId = options.id
			}

		},
		onShow() {
			that.getNoteStatistic()
			that.clearListShow(that.customerListObj)
			that.getNoteVisitData()
		},
		methods: {
			...mapMutations(['setUserInfo', 'setLoginStatus', 'setUserId', "setOrigin"]),
			changeHandler(item) {
				that.sortIndex = item.indexs[0]
				that.showSort = false
				console.log("changeHandler", item)
			},
			onShowSortType() {
				that.showSort = true
			},

			getNoteVisitData() {
				if (that.customerListObj.curPageIndex != that.customerListObj.pageIndex) {
					that.customerListObj.curPageIndex = that.customerListObj.pageIndex
					api.getNoteVisitData({
						param: {
							id: that.nodeId,
							limit: 10,
							page: that.customerListObj.pageIndex,
						}
					}).then(res => {
						console.log("getNoteVisitData", res)
						if (res.code==0) {
							that.pushListShow(that.customerListObj, res.data)
						}
					})
				}
			},
			getNoteStatistic() {
				api.getNoteStatistic({
					param: {
						id: that.nodeId,
					}
				}).then(res => {
					console.log("getNoteStatistic", res)
					if (res.success) {
						that.nodeObj = res.data
					}
				})
			},
			/**
			 * @description: 列表中添加数据
			 * @param {number} id   u-tabs-swiper的当前ID
			 * @param {list}  列表
			 * @return void
			 */
			pushListShow(srcObj, pushlist) {
				if (pushlist && pushlist.length > 0) {
					for (let i = 0; i < pushlist.length; i++) {
						// pushlist[i].createTime = utils.formatTime(pushlist[i].createTime, 'yyyy-MM-dd')
						srcObj.list.push(pushlist[i])
					}
					srcObj.pageIndex++;

					if (pushlist.length == 10) {
						srcObj.loadStatus = "loadmore"
					} else {
						srcObj.loadStatus = "nomore"
					}
				} else {
					srcObj.loadStatus = "nomore"
				}
			},

			//----清空列表
			clearListShow(srcObj) {
				srcObj.curPageIndex = 0;
				srcObj.pageIndex = 1;
				srcObj.list.splice(0, srcObj.list.length)
			},




			gotoCustomerDetail(item) {
				//||this.isChannel==1
				if (!this.shopInfo.allowEditUser) {
					return;
				}
				if (this.isReferrer == 1) {
					return;
				}
				uni.navigateTo({
					url: '/pages/shop/customer/customerDetail?id=' + item.userId
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f3f3f3;
	}

	.headerWrapper {
		width: 702rpx;
		background-color: #ffffff;
		border-radius: 10rpx;
		padding: 20rpx;
		margin-top: 20rpx;

		.titleWrapper {
			width: 100%;
			font-size: 30rpx;
			font-weight: bold;
			color: #191919;
			line-height: 40rpx;
			margin-bottom: 20rpx;
		}

		.detail {
			width: 100%;
			font-size: 24rpx;
			line-height: 40rpx;
			color: rgba(25, 25, 25, 0.5);
			min-height: 50rpx;
			margin-bottom: 20rpx;
		}

		.content {
			border-top: 1px solid #ececec;
			height: 160rpx;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-around;

			.item {
				font-size: 40rpx;
				font-weight: bold;
				line-height: 50rpx;
				color: #17f;
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 200rpx;

				.text {
					font-size: 24rpx;
					font-weight: normal;
					color: #888;
					line-height: 30rpx;
					margin-top: 5rpx;
				}
			}
		}
	}

	.customerWrapper {
		width: 100%;
		padding: 40rpx 24rpx;

		.header {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				font-size: 28rpx;
				font-weight: bold;
				font-stretch: normal;
				line-height: 18rpx;
				letter-spacing: 0rpx;
				color: #191919;
				display: flex;
				align-items: center;
				.icon{
					background-color: #17f;
					height: 40rpx;
					width: 10rpx;
					border-radius: 6rpx;
					margin-right: 15rpx;
				}
			}

			.right {
				font-size: 26rpx;
				color: #222;
				display: flex;
				align-items: center;

				.icon {
					width: 13rpx;
					height: 10rpx;
					margin-left: 15rpx;
					background-size: 100% 100%;
					background-image: url(@/static/image/icon_note_down.png);
				}
			}
		}

		.customerItem {
			width: 702rpx;
			background-color: #ffffff;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 33rpx;
			margin-top: 30rpx;

			.left {
				width: 101rpx;
				height: 101rpx;
			}

			.right {
				width: 500rpx;

				.top {
					display: flex;
					align-items: center;
					justify-content: space-between;
					font-size: 30rpx;
					font-weight: bold;
					color: #222;

					.icon {
						width: 12rpx;
						height: 24rpx;
						background-image: url(@/static/image/icon_more.png);
					}
				}

				.middle {
					display: flex;
					align-items: center;
					margin-top: 20rpx;
					margin-bottom: 20rpx;
					.preview {
						font-size: 24rpx;
						color: #444;
						line-height: 35rpx;
						margin-right: 40rpx;

						.text {
							color: #17f;
							margin: 0 10rpx;
							font-size: 32rpx;
							font-weight: bold;
						}
					}

					.time {
						font-size: 24rpx;
						color: #444;
						line-height: 35rpx;

						.text {
							color: #17f;
							font-size: 32rpx;
							margin: 0 10rpx;
							font-weight: bold;
						}
					}

				}

				.bottom {
					font-size: 22rpx;
					color: #888;
					line-height: 30rpx;
					margin-top: 20rpx;
				}
			}
		}
	}
</style>